﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage/Site.Master" AutoEventWireup="true" CodeBehind="FunctionManagement.aspx.cs" Inherits="BEELab.FunctionManagement" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentBody" runat="server">
    <div id="wrap" class="clearfix">
        <div class="block">
            <h2 class="headline">Function Management</h2>
            <div class="dashboard-hold">

                <div id="new-function" style="width: 640px; padding: 24px; border: 1px solid #ddd; display: none">
                    <asp:Label ID="lblNew" runat="server" Visible="true"> <h2 class="headline">New Function</h2></asp:Label>

                    <div class="sign-form">
                        <div class="input-hold">
                            <asp:Label ID="Label1" CssClass="sign-label" AssociatedControlID="txtName" runat="server">Name</asp:Label>
                            <asp:TextBox CssClass="sign-input" ID="txtName" runat="server"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvtxtName" CssClass="field-validation-error" ControlToValidate="txtName" ValidationGroup="AddExperimentGroup" runat="server" ErrorMessage="* Name is required." Text="*"></asp:RequiredFieldValidator>
                        </div>
                        <div class="input-hold">
                            <asp:Label ID="Label2" CssClass="sign-label" AssociatedControlID="txtDescription" runat="server">Description</asp:Label>
                            <asp:TextBox CssClass="text-area" ID="txtDescription" runat="server" TextMode="MultiLine"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="rfvtxtDescription" Display="Dynamic" ControlToValidate="txtDescription" ValidationGroup="AddExperimentGroup" runat="server" ErrorMessage="* Description is required" Text="*"></asp:RequiredFieldValidator>
                        </div>

                        <div class="input-hold">
                            <asp:Label ID="Label3" CssClass="sign-label" AssociatedControlID="ddlFunction" runat="server">Duplicate From Function</asp:Label>
                            <asp:DropDownList CssClass="sign-select" ID="ddlFunction" runat="server" DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="ID" AppendDataBoundItems="true">
                                <asp:ListItem Selected="True" Value="0" Text="Blank"></asp:ListItem>
                            </asp:DropDownList>
                            <asp:SqlDataSource ID="SqlDataSource2" ConnectionString="<%$ ConnectionStrings:beelabConnectionString %>" SelectCommand="SELECT ID, Name FROM Programs WHERE (OwnerID=@OwnerID OR Permission='Public') AND Type='Function' AND isVisible='true';" runat="server">
                                <SelectParameters>
                                    <asp:SessionParameter Name="OwnerID" SessionField="UserID" Type="Int32" />
                                </SelectParameters>
                            </asp:SqlDataSource>
                            <asp:RequiredFieldValidator ID="rfvddlFunction" ControlToValidate="ddlFunction" InitialValue="Choose" ForeColor="Red" ValidationGroup="AddExperimentGroup" runat="server" ErrorMessage="* Function is required" Text="*"></asp:RequiredFieldValidator>
                        </div>

                        <div class="input-hold">
                            <asp:Button CssClass="sign-submit" ValidationGroup="AddExperimentGroup" runat="server" ID="btnSubmit" Text="Submit" OnClick="btnSubmit_Click" OnClientClick="if (Page_ClientValidate('AddExperimentGroup')) { $.fancybox.open({'content':'Please Wait...'}); }"/>
                        </div>
                    </div>
                    <div class="error">
                        <asp:ValidationSummary runat="server" ID="vsNewExperiment" ValidationGroup="AddExperimentGroup" CssClass="field-validation-error" />
                        <p>&nbsp;</p>
                    </div>
                </div>

                <h3>Function List</h3>
                <a href="#new-function" class="abbtn fancybox">New Function</a>
            </div>
            <asp:Label ID="lblUpdateMessage" runat="server" Visible="false" ForeColor="Red"></asp:Label>
            <asp:GridView ID="gvExperiment" runat="server" AutoGenerateColumns="False" CssClass="dash-col" DataSourceID="SqlDataSource1" DataKeyNames="ID" OnRowDeleting="gvExperiment_RowDeleting" AllowPaging="True" AllowSorting="True" OnRowCommand="gvExperiment_RowCommand">
                <Columns>
                    <asp:BoundField DataField="ID" HeaderText="ID" ItemStyle-Width="8%" SortExpression="ID" InsertVisible="False" ReadOnly="True" Visible="False" >
                    <ItemStyle Width="8%" />
                    </asp:BoundField>
                    <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                    <asp:BoundField DataField="Description" HeaderText="Description" SortExpression="Description" />
                    <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" />
                    <asp:BoundField DataField="Permission" HeaderText="Permission" SortExpression="Permission" />
                    <asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="EditFunction.aspx?ID={0}" Text="Edit Information" />
                    <asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="EditFunctionExecution.aspx?ID={0}" Text="Edit Execution" />
                    <asp:HyperLinkField DataNavigateUrlFields="ID" DataNavigateUrlFormatString="EditFunctionSettings.aspx?ID={0}" Text="Edit Settings" />
                    <asp:ButtonField CommandName="MakePublic" Text="Make Public" />
                    <asp:ButtonField CommandName="MakePrivate" Text="Make Private" />
                    <asp:CommandField ShowDeleteButton="True" />
                </Columns>
            </asp:GridView>
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:beelabConnectionString %>" SelectCommand="SELECT [Programs].[ID],[Name],[Description],[Users].[Email] AS Owner, [Permission] FROM [Programs] JOIN [Users] ON Programs.OwnerID=Users.ID WHERE ( (([OwnerID] = @OwnerID) OR ('True'=@IsAdmin)) AND ([Type] = @Type) AND (([Permission]=@Permission) OR ('True'=@IsAdmin)) ) AND isVisible='true'" DeleteCommand="DELETE FROM Programs WHERE ID=@Param1 AND NOT Exists(SELECT ID FROM Containers WHERE FunctionID=@Param1); UPDATE Programs SET isVisible='false' WHERE ID=@Param1; DELETE FROM Programs WHERE Type='Function' AND isVisible='false' AND NOT EXISTS (SELECT ID FROM Containers WHERE FunctionID=Programs.ID)">
                <DeleteParameters>
                    <asp:ControlParameter ControlID="gvExperiment" Name="Param1" PropertyName="SelectedValue" />
                </DeleteParameters>
                <SelectParameters>
                    <asp:SessionParameter Name="OwnerID" SessionField="UserID" Type="Int32" />
                    <asp:SessionParameter Name="IsAdmin" SessionField="Admin" Type="String" />
                    <asp:Parameter DefaultValue="Function" Name="Type" Type="String" />
                    <asp:Parameter DefaultValue="Private" Name="Permission" Type="String" />
                </SelectParameters>
            </asp:SqlDataSource>
        </div>
    </div>

    <script type="text/javascript">
        $(document).ready(function () {
            $("a[class='abbtn fancybox']").fancybox();

            $('a[href*="EditFunction.aspx?ID="]').fancybox({ 'type': 'iframe', 'width': '705px' });

            $('a').filter(function (index) { return $(this).text() === "Delete"; }).click(function () { return confirm('Are you sure you want to delete this item?'); })
            $('a').filter(function (index) { return $(this).text() === "Make Public"; }).click(function () { return confirm('If you make this function public you will no longer own it. Are you sure you want to proceed?'); })
        });
    </script>

</asp:Content>
